package com.mikao.controller.goods;

import com.mikao.dto.bo.*;
import com.mikao.dto.vo.*;
import com.mikao.entity.Type;
import com.mikao.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author KennyDavid
 */

@Slf4j
@RestController
@RequestMapping("/management")
@Scope("prototype")
public class GoodsController {

    private GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @GetMapping("/goodsList")
    public RspDataTableDto<GoodsDto> goodsList(ReqDataTableDto dataTableDTO,
                                               @RequestParam(required = false) String nameOrMobile, @RequestParam(required = false) String dateRangePicker,
                                               @RequestParam(required = false) Long type) {
        return goodsService.goodsList(dataTableDTO, nameOrMobile, dateRangePicker, type);
    }

    @PostMapping("/saveGoods")
    public void saveGoods(GoodsForm goodsForm, @RequestParam(required = false) MultipartFile coverImage) {
        goodsService.saveGoods(goodsForm, coverImage);
    }

    @PostMapping("/removeGoods/{id}")
    public void removeGoods(@PathVariable Long id) {
        goodsService.removeGoods(id);
    }

    @PostMapping("/updateGoodsStatus")
    public boolean updateGoodsStatus(Boolean status, @RequestParam(value = "id[]") Long... id) {
        return goodsService.updateGoodsStatus(status, id);
    }

    @PostMapping("/uploadArticleImg")
    public ImageDto uploadArticleImg(@RequestParam("file") MultipartFile file) {
        return goodsService.saveImage(file);
    }

    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ImagesDto uploadFile(@RequestParam MultipartFile... file) {
        return goodsService.uploadFile(file);
    }

    @GetMapping("/treeList")
    public RspDataTableDto<TreeDto> treeList(ReqDataTableDto reqDataTableDto, TreeForm treeForm) {
        return goodsService.treeList(reqDataTableDto, treeForm);
    }

    @PostMapping("/addTree")
    public void addTree(TreeEditForm treeEditForm) {
        goodsService.addTree(treeEditForm);
    }

    //平台收购果树订单
    @GetMapping("/recycleTreeOrder")
    public RspDataTableDto<TreeOrderDto> recycleTreeOrder(ReqDataTableDto reqDataTableDto, TreeForm treeForm) {
        return goodsService.recycleTreeOrder(reqDataTableDto, treeForm);
    }

    //平台收购果树报价
    @PostMapping("/addTreePrice")
    public void addTreePrice(TreePriceForm treePriceForm) {
        goodsService.addTreePrice(treePriceForm);
    }

    //平台收购果实订单
    @GetMapping("/recycleFruitOrder")
    public RspDataTableDto<FruitOrderDto> recycleFruitOrder(ReqDataTableDto reqDataTableDto, TreeForm treeForm) {
        return goodsService.recycleFruitOrder(reqDataTableDto, treeForm);
    }

    //植友交易记录/用户果实市场
    @GetMapping("/fruitMarketList")
    public RspDataTableDto<FruitMarketDto> fruitMarketList(ReqDataTableDto reqDataTableDto, TreeForm treeForm) {
        return goodsService.fruitMarketList(reqDataTableDto, treeForm);
    }

    //给用户间交易的水果发货
    @PostMapping("/sendFruitMarket")
    public void sendFruitMarket(SendForm sendForm) {
        goodsService.sendFruitMarket(sendForm);
    }

    //植友间果树交易记录/用户果树市场
    @GetMapping("/treeMarketList")
    public RspDataTableDto<TreeMarketDto> treeMarketList(ReqDataTableDto reqDataTableDto, TreeForm treeForm) {
        return goodsService.treeMarketList(reqDataTableDto, treeForm);
    }
}
